<?php

class listCategoryTree {
    private $catalog_category;
    
    public function __construct() {
        $this->catalog_category = CatalogCategory::getAllCategoryCurrentLanguage();
        
        
        
    }
    
    public function getResult(){
        if (count($this->catalog_category)>0)
        {
            $this->catalog_category = $this->structuring($this->catalog_category);
            $result = $this->build_tree($this->catalog_category,0);
        }
        return $result;
    }

        private function structuring(array $listIdParent){
        foreach ($listIdParent as $cat) {
             $cats[$cat['parent_id']][] =  $cat;
        }
        return $cats;
    }
    
    private function  build_tree($cats,$parent_id){

      if(is_array($cats) and  count($cats[$parent_id])>0){

        $tree = '<ul>';

        foreach($cats[$parent_id] as $cat){

           $tree .= '<li>'.  CHtml::link($cat['title'],Yii::app()->createUrl('catalog/default/index',array('url'=>$cat['url'])));

           $tree .=  $this->build_tree($cats,$cat['id']);

           $tree .= '</li>';         

        }

        $tree .= '</ul>';

      } 
      else return null;          

      return $tree;        
    }
}